#include<bits/stdc++.h>
using namespace std;


class Solution {
public:
    const int INF=INT_MAX/2;
    long long minimumCost(string source, string target, vector<char>& original, vector<char>& changed, vector<int>& cost) {
        char ch='a';
        int n=26;
        vector<vector<int>> f(n,vector<int>(n,INF));
        int l=cost.size();
        for(int i=0;i<n;++i) f[i][i]=0;
        for(int i=0;i<l;++i)
        {
            char a=original[i],b=changed[i];
            int k=cost[i];
            f[a-'a'][b-'a']=min(k,f[a-'a'][b-'a']);
        }
        for(int k=0;k<n;++k)
        {
            for(int i=0;i<n;++i)
            {
                if(f[i][k]>=INF) continue;
                for(int j=0;j<n;++j)
                {
                    f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
                }
            }
        }
        long long ans=0;
        for(int i=0;i<source.size();++i)
        {
            char a=source[i],b=target[i];
            if(f[a-'a'][b-'a']>=INF) return -1;
            else ans+=f[a-'a'][b-'a'];
        }
        return ans;
    }
};